import os

import pandas as pd
import realtime_vote


# 调用realtime_vote模块，获取投票数据
res4= realtime_vote.get_vote()
now_time= res4["jData"]["nowtime"]
cvote= res4["jData"]["coachvotes"]


# 导入Excel表格，各列均转换成列表
mlist = pd.read_excel("data/教练清单.xlsx")

id = mlist["id"].tolist()
name = mlist["name"].tolist()
pic = mlist["pic"].tolist()
vote = mlist["vote"].tolist()

# 将列表id转为字符
for i in range(len(id)):
    id[i] = str(id[i])


# 根据cvote的id匹配教练，更新vote列表
for i in cvote:   
    j= id.index(i)
    vote[j]= cvote[i]
 

# vote字符串转数字
for i,j in enumerate(vote):
    vote[i] = int(vote[i])



# 构造matrix表格
def matrix (id, name, pic, vote):   
    matrix5 = {
        "id":id,
        "name":name,
        "pic":pic,
        "vote":vote
        }
    df = pd.DataFrame(matrix5)
    return df

# 生成Pandas表格
df7= matrix(id, name, pic, vote)

# 按照得票排序
(row, column) = df7.shape

df8= df7.nlargest(row,"vote")
df9= df8.reset_index()

# 导出文件Excel
file_name6 = "教练清单" + str(now_time) + ".xlsx"
file_path6 = './data/'
full_path = os.path.join(file_path6, str(file_name6))
df9.to_excel(full_path, sheet_name="coachs", index=True)

 

# 导出为HTML字符串
html9 = df9.to_html()
 
# 打印HTML字符串
print(html9)
 
# 如果想要将HTML保存到文件中
with open('./to_html/coachs_vote.html', 'w') as f:
    f.write(html9)











